use std::{env,fs};
use pest_derive::Parser;
use pest::Parser;

#[derive(Parser)]
#[grammar = "lexer.pest"]
struct SysYLexer;

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }

    let filename = &args[1];
    let input = match fs::read_to_string(filename) {
        Ok(content) => content,
        Err(e) => {
            eprintln!("Error reading file {}: {}", filename, e);
            std::process::exit(1);
        }
    };

    match SysYLexer::parse(Rule::main_rule, &input) {
        Ok(pairs) => {
            for pair in pairs {
                for inner_pair in pair.into_inner() {
                    match inner_pair.as_rule() {
                        Rule::keyword_const => eprintln!("const: const"),
                        Rule::keyword_int => eprintln!("int: int"),
                        Rule::keyword_void => eprintln!("void: void"),
                        Rule::keyword_if => eprintln!("if: if"),
                        Rule::keyword_else => eprintln!("else: else"),
                        Rule::keyword_while => eprintln!("while: while"),
                        Rule::keyword_break => eprintln!("break: break"),
                        Rule::keyword_continue => eprintln!("continue: continue"),
                        Rule::keyword_return => eprintln!("return: return"),
                        Rule::op_plus => eprintln!("PLUS: +"),
                        Rule::op_minus => eprintln!("MINUS: -"),
                        Rule::op_mul => eprintln!("MUL: *"),
                        Rule::op_div => eprintln!("DIV: /"),
                        Rule::op_mod => eprintln!("MOD: %"),
                        Rule::op_assign => eprintln!("ASSIGN: ="),
                        Rule::op_eq => eprintln!("EQ: =="),
                        Rule::op_neq => eprintln!("NEQ: !="),
                        Rule::op_lt => eprintln!("LT: <"),
                        Rule::op_gt => eprintln!("GT: >"),
                        Rule::op_le => eprintln!("LE: <="),
                        Rule::op_ge => eprintln!("GE: >="),
                        Rule::op_not => eprintln!("NOT: !"),
                        Rule::op_and => eprintln!("AND: &&"),
                        Rule::op_or => eprintln!("OR: ||"),
                        Rule::paren_l => eprintln!("L_PAREN: ("),
                        Rule::paren_r => eprintln!("R_PAREN: )"),
                        Rule::brace_l => eprintln!("L_BRACE: {{"),
                        Rule::brace_r => eprintln!("R_BRACE: }}"),
                        Rule::bracket_l => eprintln!("L_BRACKT: ["),
                        Rule::bracket_r => eprintln!("R_BRACKT: ]"),
                        Rule::comma => eprintln!("COMMA: ,"),
                        Rule::semicolon => eprintln!("SEMICOLON: ;"),
                        Rule::IDENT => {
                            let ident_str = inner_pair.as_str();
                            eprintln!("IDENT: {}", ident_str);
                        }
                        Rule::INTEGER_CONST => {
                            let int_str = inner_pair.as_str();
                            let value = if int_str.starts_with("0x") || int_str.starts_with("0X") {
                                i64::from_str_radix(&int_str[2..], 16).unwrap_or(0)
                            } else if int_str.starts_with('0') && int_str.len() > 1 {
                                i64::from_str_radix(&int_str[1..], 8).unwrap_or(0)
                            } else {
                                int_str.parse::<i64>().unwrap_or(0)
                            };
                            eprintln!("INTEGER_CONST: {}", value);
                        }
                        _ => {} // 忽略其他规则，例如注释和空白符
                    }
                }
            }
        }
        Err(e) => {
    eprintln!("Parse error:\n{}", e);
    std::process::exit(1);
}
    }
}


